var JImageWidgetOptions = {
	uploadUrl:''
};

function JImageWidget (options){
  JWidget.apply(this,arguments);
  this.imgId = null;
}
JImageWidget.prototype.__proto__ = JWidget.prototype;

JImageWidget.prototype.init = function(){
  /*
   * 调用父类方法
   */
  JWidget.prototype.init.apply(this,arguments);

  this.fields.tag = 'img';
//  
//  this.configItems = $.extend(true,weiboConfigItems ,this.configItems);
	
   var me = this;
   var imageConfigItems = [$('<button/>').css({border:'1px solid',cursor:'pointer',height:'20px',padding:'3px 30px',margin:'10px'}).text('更新照片').click(function(){
	   		//me.imgCropBox(me.fields.src);
	   		 me.imgUpload();
		   } )];
                      	
	this.configItems = $.extend(true,imageConfigItems ,this.configItems);

};
 
	 

JImageWidget.prototype.run = function(){ 
  /*
   * 调用父类方法
   */
  JWidget.prototype.run.apply(this,arguments);
  
  if(this.fields.src){
	  this.core.attr('src',this.fields.src);
  }
  if(this.fields.imgId){
 
  
  }
 };

/**
 * src is required.
 * @return
 */
/* added later.
JImageWidget.prototype.imgCropBox = function(src){ 
	if(typeof src == 'undefined'){
		this.imgDialog();
		return;
	}
	var warpBox = $('<div/>');
	var jcropImg = jQuery('<img/>').attr('src',src).appendTo(warpBox);
	jcropImg.load(function(){ 
		jcrop = $.Jcrop(jcropImg,{
    		onChange: function(){},
    		onSelect:  function(){},
    		aspectRatio: 0,
    		boxWidth:400,
    		boxHeight:400,
    		setSelect:[ 0, 0, 200, 200 ]
    	});

	});
	
	warpBox.dialog({
		width:600,height:500
	});
	this.configBox.dialog('close');
};
*/
JImageWidget.prototype.imgUpload= function(){ 
	var me = this; 
	var data = {uid:MR8.user.uid};
	if(this.fields.imgId){
		data.imgId = this.fields.imgId;
	}
	$('<input/>').attr('type','file').attr('name','file').css({display:'',opacity:0}).appendTo(this.dom).change(function(){
		var imgFIle = $(this);
		$(this).upload(JImageWidgetOptions.uploadUrl,data, function(res) {
			
			if(res['error'] && res['error']['file']){
				alert(res['error']['file']);
				me.imgUpload();
			}
			if(res['src']){
				src = res['src'];
				src = src.replace('amp;','');

				me.field('imgId',res['imgId']);
				me.field('src',src);
				$(me.core).attr('src', src); 
			}
			imgFIle.remove();
	    } ,'json' );
//	,'json'

	}).click();
	
};

/*
 * 事件
 */
JWidget.prototype.resized = function(event, ui){};
JWidget.prototype.draged = function(event, ui){};
JWidget.prototype.move = function(event, ui ){}; 
JWidget.prototype.click = function(){}; 
JWidget.prototype.load = function(){};
JWidget.prototype.beforeSave = function(){ return true;};